﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.IO;
using iTextSharp.text.html.simpleparser;
using System.Web.UI;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Web;

namespace TCL.FUNC
{
    public class ExportEx
    {
        //Note: Cần phải thêm đoạn code này trước hàm BindData
        //public override void VerifyRenderingInServerForm(Control control)
        //{
        //    /* Verifies that the control is rendered */
        //}


        public static void Word_Export(GridView grvTemp, string filename)
        {
            if (grvTemp.PageCount > 0)
            {
                System.Web.HttpContext.Current.Response.Clear();
                System.Web.HttpContext.Current.Response.Buffer = true;
                System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".doc");
                System.Web.HttpContext.Current.Response.Charset = "";
                System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-word ";
                StringWriter sw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw);
                grvTemp.AllowPaging = false;
                grvTemp.DataBind();
                grvTemp.RenderControl(hw);
                System.Web.HttpContext.Current.Response.Output.Write(sw.ToString());
                System.Web.HttpContext.Current.Response.Flush();
                System.Web.HttpContext.Current.Response.End();
            }
        }

        public static void Excel_Export(GridView grvTemp, string filename)
        {
            if (grvTemp.PageCount > 0)
            {
                System.Web.HttpContext.Current.Response.Clear();
                System.Web.HttpContext.Current.Response.Buffer = true;
                System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".xls");
                System.Web.HttpContext.Current.Response.Charset = "";
                System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
                StringWriter sw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw);
                grvTemp.AllowPaging = false;
                grvTemp.DataBind();
                for (int i = 0; i < grvTemp.Rows.Count; i++)
                {
                    GridViewRow row = grvTemp.Rows[i];
                    //Apply text style to each Row
                    row.Attributes.Add("class", "textmode");
                }
                grvTemp.RenderControl(hw);

                //style to format numbers to string
                string style = @"<style> .textmode { mso-number-format:\@; } </style>";
                System.Web.HttpContext.Current.Response.Write(style);
                System.Web.HttpContext.Current.Response.Output.Write(sw.ToString());
                System.Web.HttpContext.Current.Response.Flush();
                System.Web.HttpContext.Current.Response.End();
            }
        }

        public static void PDF_Export(GridView grvTemp, string filename)
        {
            if (grvTemp.PageCount > 0)
            {
                System.Web.HttpContext.Current.Response.ContentType = "application/pdf";
                System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".pdf");
                System.Web.HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                StringWriter sw = new StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw);
                grvTemp.AllowPaging = false;
                grvTemp.DataBind();
                grvTemp.RenderControl(hw);
                StringReader sr = new StringReader(sw.ToString());
                Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
                HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
                PdfWriter.GetInstance(pdfDoc, System.Web.HttpContext.Current.Response.OutputStream);
                pdfDoc.Open();
                htmlparser.Parse(sr);
                pdfDoc.Close();
                System.Web.HttpContext.Current.Response.Write(pdfDoc);
                System.Web.HttpContext.Current.Response.End();
            }
        }
    }
}
